package com.gonetbar.base.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.log4j.Logger;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.gonetbar.base.dao.BaseDao;
import com.gonetbar.base.mail.SpeedAdminJavaMailSender;

/**
 * 
 * @Description:
 * 
 * @author weisd Email:xiyangdewuse@163.com
 * @date 2011-1-5 下午09:13:30
 * @version v1.0
 */
public class BaseDaoImpl extends SqlSessionDaoSupport implements BaseDao {

	// TODO 怎么 使用拦截器进行发送邮件

	private Logger log = Logger.getLogger(BaseDaoImpl.class);
	private SpeedAdminJavaMailSender sqlWaringJavaMailSender;
	protected int waringTime = 25000;
	private boolean timeOutWaring = true;
	private boolean sqlExceptionWaring = false;

	public Object addObject(String sqlid, Object obj) {
		Date before = new Date();
		Object o = this.getSqlSession().insert(sqlid, obj);
		Date after = new Date();
		int woTime = (int) (after.getTime() - before.getTime());
		if (woTime > waringTime) {
			this.seedSqlWaringEMail(sqlid, woTime);
		}
		return o;
	}

	public Object findObject(String sqlid, Object obj) {
		Date before = new Date();
		Object o = this.getSqlSession().selectOne(sqlid, obj);
		Date after = new Date();
		int woTime = (int) (after.getTime() - before.getTime());
		if (woTime > waringTime) {
			this.seedSqlWaringEMail(sqlid, woTime);
		}
		return o;
	}

//	public PageBean queryListPage(String countSqlid, String dataSqlid,
//			BasePageVObj obj, int currentlyPage, int pageSize) {
//
//		Map statMap = null;
//		Date before = new Date();
//		// statMap = (Map) getSqlMapClientTemplate().queryForObject(countSqlid,
//		// obj);
//		statMap = this.getSqlSession().selectMap(countSqlid, obj, null);
//
//		PageBean pb = null;
//		try {
//			pb = new PageBean(currentlyPage, pageSize, statMap);
//		} catch (Exception e) {
//			e.printStackTrace();
//			this.seedSqlExcetpionEMail(countSqlid, e);
//			return null;
//		}
//		obj.setStart(pb.getStart());
//		obj.setEnd(pb.getEnd());
//
//		List dataList = null;
//		dataList = this.getSqlSession().selectList(dataSqlid, obj);
//		pb.setDataList(dataList);
//		Date after = new Date();
//		int woTime = (int) (after.getTime() - before.getTime());
//		if (woTime > waringTime) {
//			this.seedSqlWaringEMail(dataSqlid, woTime);
//		}
//		return pb;
//	}

	public List queryList(String sqlid, Object obj) {
		List tempList = null;
		Date before = new Date();
		tempList = this.getSqlSession().selectList(sqlid, obj);
		Date after = new Date();
		int woTime = (int) (after.getTime() - before.getTime());
		if (woTime > waringTime) {
			this.seedSqlWaringEMail(sqlid, woTime);
		}
		return tempList;
	}

	public int deleteObject(String sqlid, Object obj) {
		Date before = new Date();
		int ac = this.getSqlSession().delete(sqlid, obj);
		Date after = new Date();
		int woTime = (int) (after.getTime() - before.getTime());
		if (woTime > waringTime) {
			this.seedSqlWaringEMail(sqlid, woTime);
		}
		return ac;
	}

	public int updateObject(String sqlid, Object obj) {
		Date before = new Date();
		int ac = this.getSqlSession().update(sqlid, obj);
		Date after = new Date();
		int woTime = (int) (after.getTime() - before.getTime());
		if (woTime > waringTime) {
			this.seedSqlWaringEMail(sqlid, woTime);
		}
		return ac;
	}

	public String getSql(String sqlid, Object obj) {
		Configuration cfg = this.getSqlSession().getConfiguration();
		MappedStatement ms = cfg.getMappedStatement(sqlid);
		if (null == ms) {
			return null;
		}
		BoundSql bs = ms.getBoundSql(obj);
		return bs.getSql();
	}

	public String getSql(String sqlid) {
		return this.getSql(sqlid, null);
	}

	protected void seedSqlExcetpionEMail(String sqlid, Exception e) {
		StringBuffer logInfo = new StringBuffer();
		// logInfo.append("DataSource:");
		// logInfo.append(ds);
		logInfo.append(" Sqlid:");
		logInfo.append(sqlid);
		logInfo.append(" SQL:");
		// if (ds != null)
		logInfo.append(this.getSql(sqlid));
		log.error(logInfo.toString(), e);
		logInfo.append(" Message:");
		logInfo.append(e.getMessage());
		e.printStackTrace();
		if (!sqlExceptionWaring || sqlWaringJavaMailSender == null)
			return;
		sqlWaringJavaMailSender.getSpeedAdminEmailMessage().setText(
				logInfo.toString());
		sqlWaringJavaMailSender.senderMail();
	}

	protected void seedSqlWaringEMail(String sqlid, int woTime) {
		if (!timeOutWaring || sqlWaringJavaMailSender == null)
			return;
		try {
			StringBuffer logInfo = new StringBuffer();
			// logInfo.append("DataSource:");
			// logInfo.append(ds);
			logInfo.append(" Sqlid:");
			logInfo.append(sqlid);
			logInfo.append(" SQL:");
			// if (ds != null)
			logInfo.append(this.getSql(sqlid));
			logInfo.append(" Message: SQL executing wear-out time beyond > ");
			logInfo.append(waringTime);
			logInfo.append(" ms!!achieved wear-out time:");
			logInfo.append(woTime);
			sqlWaringJavaMailSender.getSpeedAdminEmailMessage().setText(
					logInfo.toString());
			sqlWaringJavaMailSender.senderMail();
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}

	public boolean isTimeOutWaring() {
		return timeOutWaring;
	}

	public void setTimeOutWaring(boolean timeOutWaring) {
		this.timeOutWaring = timeOutWaring;
	}

	public boolean isSqlExceptionWaring() {
		return sqlExceptionWaring;
	}

	public void setSqlExceptionWaring(boolean sqlExceptionWaring) {
		this.sqlExceptionWaring = sqlExceptionWaring;
	}

	public int getWaringTime() {
		return waringTime;
	}

	public void setWaringTime(int waringTime) {
		this.waringTime = waringTime;
	}

	public SpeedAdminJavaMailSender getSqlWaringJavaMailSender() {
		return sqlWaringJavaMailSender;
	}

	public void setSqlWaringJavaMailSender(
			SpeedAdminJavaMailSender sqlWaringJavaMailSender) {
		this.sqlWaringJavaMailSender = sqlWaringJavaMailSender;
	}

}
